/******************** 
	作用:公共菜单
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-09
********************/

(function(window) {

	function GlobalMenu() {
		this.sourceMenuData = null;//静态菜单数据
		try {
			if(sourceMenuData!=undefined)
				this.sourceMenuData = sourceMenuData;
		} catch (e) {
//			Common.log(e);
		}
		
		this.parentId = "";
//		this.url = "../action/ListMenu.aspx"; //获取数据的网址
		this.url = "/getMenu"; //获取数据的网址
		try {
			if(sourceMenuUrl!=undefined)
				this.url = sourceMenuUrl;
		} catch (e) {
//			Common.log(e);
		}
		
		this.menuData = []; //保存菜单数据
		this.menuIdPrefix = "side-menu-"; //菜单ID前缀
		this.breadcrumbIdPrefix = "breadcrumb-"; //导航ID前缀
		this.defaultIconClass = "menu-icon fa fa-tachometer"; //默认图标
		this.arrowIconClass = "menu-icon fa fa-caret-right"; //箭头图标

		this.hasInit = false; //判断是否已经初始化,(初始化已经打开页面,此时不需要再重新刷新页面)
		this.isRefresh = false; //判断是否点击刷新按钮
	}

	GlobalMenu.prototype = {
		constructor: GlobalMenu,

		/**
		 * 初始化菜单
		 */
		init: function() {
			try {
				//获取所属父级菜单的ID
				this.parentId = $("#id-sidebar-menu").attr("parentId");
				this.startGetMenuData();
			} catch (e) {
				Common.log(e);
			}
		},
		/**
		 * 刷新菜单
		 */
		refresh: function() {
			try {
				this.isRefresh = true; //判断是否点击刷新按钮
				this.startGetMenuData();
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 开始获取菜单数据
		 */
		startGetMenuData: function() {
			try {
				if (this.sourceMenuData != undefined && this.sourceMenuData != null) {
					this.parseMenuData(this.sourceMenuData);
					this.showCurrentMenu();
				} else {
					//获取菜单数据
					var options = {
						"self": this,
						"id": this.parentId
					};
					var url = this.url;
					if(this.parentId){
						url=Common.addUrlParameters(url, {"id": this.parentId});
					}
					var data = {
						"id": this.parentId
					};
					data=null;
					var tip = "正在获取菜单数据";
					Common.ajaxPost(options, null, url, data, tip, this.completeGetMenuData, this.failGetMenuData,"GET");
				}
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 获取菜单数据成功
		 * @param {Object} options 选项
		 * @param {Object} data
		 * @param {Object} status
		 */
		completeGetMenuData: function(options, data, status) {
			try {
				data = $.parseJSON(data).data;
				options.self.parseMenuData.call(options.self, data);
				options.self.showCurrentMenu();
				//				this.parseMenuData(options.self,data);
				//				console.log(this);
				//				options.self.parseMenuData(options.self,data);
				//				Common.log(data);
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 获取菜单数据失败
		 * @param {Object} options 选项
		 */
		failGetMenuData: function(options) {
			try {
				Common.log("获取菜单数据失败,请点击刷新按钮重新获取");
			} catch (e) {
				Common.log(e);
			}
		},


		/**
		 * 将指定ID的菜单设置为当前菜单
		 * @param {String} id 菜单id
		 */
		showCurrentMenu: function() {
			//将指定ID的菜单设置为当前菜单
			try {
				var id = $("#id-sidebar-menu").attr("currentId");
				if (id) {
					var selector = "#" + this.menuIdPrefix + id + ">a";
					this.clickMenu($(selector));
				}
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 点击菜单
		 * @param {Object} menu 被点击的菜单(a元素)
		 */
		clickMenu: function(menu) {
			var self = this;
			//			alert(this.menuIdPrefix);
			//清除所有菜单的打开和激活类
			//如果当前菜单是打开的则将其关闭
			if (menu.hasClass("dropdown-toggle")) {
				//当前有子菜单
				//判断子级是否为激活状态,如果是则保留原状态,不要清除
				if (menu.closest("li").find(".submenu:eq(0) li.active").length > 0) {
					//往上查找父级,将其所有父级的子菜单显示出来
					menu.parents(".submenu").removeClass("nav-hide");
					//将其所有父级都设置为打开状态
					menu.closest("li").parents("li").addClass("open");

					if (menu.closest("li").hasClass("open")) {
						//当前为打开状态,将其关闭
						menu.closest("li").toggleClass("open").find(".submenu:eq(0)").addClass("nav-hide");
					} else {
						menu.closest("li").toggleClass("open").find(".submenu:eq(0)").removeClass("nav-hide");
					}
				} else {
					//判断上一次是否当前菜单处于打开状态
					if (menu.closest("li").hasClass("open")) {
						//当前为打开状态,将其关闭
						menu.closest("li").removeClass("open").find(".submenu:eq(0)").addClass("nav-hide");
					} else {
						//当前是其它菜单处于激活状态
						$("#id-sidebar-menu li").removeClass("active open");
						$("#id-sidebar-menu .submenu").removeClass("nav-hide");
						menu.parents('li').addClass("active open");
					}
				}
			} else {
				//当前无子级,直接设为当前菜单
				$("#id-sidebar-menu li").removeClass("active open");
				$("#id-sidebar-menu .submenu").removeClass("nav-hide");
				var allLi = menu.parents('li');
				if (allLi.length > 0) {
					allLi.addClass("active open").first().removeClass("open");
				} else {
					allLi.addClass("active open");
				}
				//保存当前ID
				var currentId = menu.closest("li").attr("id").replace(self.menuIdPrefix, "");
				$("#id-sidebar-menu").attr("currentId", currentId);
				//------------------显示当前链接
				var breadcrumbData = [];
				var obj;
				allLi.each(function(key, value) {
					var id = $(this).attr("id").replace(self.menuIdPrefix, "");
					var text = $(this).find(".menu-text:eq(0)").html();
					var href = $(this).find("a:eq(0)").attr("href");
					obj = {
						"id": id,
						"text": text,
						"href": href,
					};
					breadcrumbData.unshift(obj);
				});
				this.showBreadcrumb(breadcrumbData); //显示导航
				//------------------打开链接
				var url = menu.attr("href");
				if (url != "" && url != "#") {
					if (this.isRefresh && this.hasInit) {
						//点击刷新按钮只时刷新菜单,而不重新打开页面

					} else {
//						url = Common.addUrlParameters(url, {
//							"dt": Math.random() + ""
//						});
						//						url += "?dt=" + Math.random();
						this.hasInit = true;
						$("#top-container").attr("src", url);
					}
				}
			}

		},

		/**
		 * 初始化监听事件
		 */
		initListener: function() {
			try {
				var self = this;
				//获取菜单数据
				$("#id-sidebar-menu li a").on("click", function(e) {
					General.stopEvent(e); //停止事件冒泡
					self.isRefresh = false; //判断是否点击刷新按钮
					self.clickMenu($(this));
				});
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 清除监听事件
		 */
		removeListener: function() {
			try {
				$("#id-sidebar-menu li a").off();
			} catch (e) {
				Common.log(e);
			}
		},

		/**
		 * 显示导航
		 * @param {Object} menuData 菜单数据来源
		 */
		showBreadcrumb: function(menuData) {
			var str = "";
			var self = this;
			$.each(menuData, function(index, obj) {
				if (index < menuData.length - 1) {
					str += "<li>";
				} else {
					str += "<li class=\"active\">";
				}
				if (index == 0)
					str += "<i class=\"ace-icon fa fa-home home-icon\"></i>";
				if (index < menuData.length - 1) {
					str += "<a remark=\"" + obj.id + "\" href=\"" + obj.href + "\">" + obj.text + "</a>";
				} else {
					str += "<a remark=\"" + obj.id + "\" href=\"" + obj.href + "\">" + obj.text + "</a>";
					//					str += obj.text;
				}

				str += "</li>";
			});
			$("#id-breadcrumb a").off();
			$("#id-breadcrumb").html(str);
			$("#id-breadcrumb a").on("click", function(e) {
				e.stopImmediatePropagation();
				e.stopPropagation();
				e.preventDefault();
				var id = $(this).attr("remark");
				//				var id=$(this).attr("remark").replace(this.breadcrumbIdPrefix,"");
				var selector = "#" + self.menuIdPrefix + id + ">a";
				self.isRefresh = false; //判断是否点击刷新按钮
				self.clickMenu($(selector));
			});
		},

		/**
		 * 解析菜单数据
		 * @param {Object} menuData 菜单数据来源
		 */
		parseMenuData: function(menuData) {
			var self = this;
			//			console.log(this.getChildMenuData(menuData[0].child));
			//			alert(menuData);
			//		var id = attr.currentId; //当前菜单ID
			var str = "";
			$.each(menuData, function(index, topMenu) {
				//			angular.forEach(menuData, function(topMenu, key) {
				var tempStr = '';
				tempStr += '<li class="" id="' + self.menuIdPrefix + topMenu.id + '">';
				if (topMenu.child.length > 0) {
					tempStr += '	<a href="' + topMenu.href + '" class="dropdown-toggle">';
				} else {
					tempStr += '	<a href="' + topMenu.href + '">';
				}
				tempStr += '		<i class="' + topMenu.iconClass + '"></i>';
				tempStr += '		<span class="menu-text">' + topMenu.text + '</span>';
				//有子菜单,显示下拉图标
				if (topMenu.child.length > 0) {
					tempStr += '	<b class="arrow fa fa-angle-down"></b>';
				}
				tempStr += '	</a>';
				tempStr += '	<b class="arrow"></b>';


				//有子菜单
				if (topMenu.child.length > 0) {
					tempStr += self.getChildMenuData(topMenu.child);
				}
				tempStr += '</li>';
				str += tempStr;
			});

			//将菜单展示出来
			this.removeListener();
			$("#id-sidebar-menu").html(str);
			this.initListener(); //初始化监听事件
		},

		/**
		 * 获取子菜单数据
		 * @param {Object} menus 子级菜单数据
		 */
		getChildMenuData: function(menus) {
			var self = this;
			var str = '<ul class="submenu">';
			$.each(menus, function(index, currentMenu) {

				//			angular.forEach(menus, function(currentMenu, key) {
				var tempStr = '';
				tempStr += '<li class="" id="' + self.menuIdPrefix + currentMenu.id + '">';
				//有子菜单,点击可以展开菜单
				if (currentMenu.child.length > 0) {
					tempStr += '	<a href="' + currentMenu.href + '" class="dropdown-toggle">';
				} else {
					tempStr += '	<a href="' + currentMenu.href + '">';
				}
				tempStr += '		<i class="' + currentMenu.iconClass + '"></i>';
				tempStr += '		<span class="menu-text">' + currentMenu.text + '</span>';
				//有子菜单,显示下拉图标
				if (currentMenu.child.length > 0) {
					tempStr += '	<b class="arrow fa fa-angle-down"></b>';
				}
				tempStr += '	</a>';
				tempStr += '	<b class="arrow"></b>';
				//有子菜单
				if (currentMenu.child.length > 0) {
					tempStr += self.getChildMenuData(currentMenu.child);
				}
				tempStr += '</li>';
				str += tempStr;
			});
			str += "</ul>";
			return str;
		}
	}

	//--------------------------------
	if (window.GlobalMenu == undefined) {
		if (window.top.GlobalMenu == undefined)
			window.GlobalMenu = GlobalMenu;
		else
			window.GlobalMenu = window.top.GlobalMenu;
	}
	//--------------------------------
})(window);